import { observable, action } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
// const {TableStore} = WeaTableNew;
import * as Apis from "../apis/index"
import { toJS } from 'mobx';
import {WeaTools} from "ecCom"
import {Modal ,message} from "antd"
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

 
export  class ManagerStore{
    @observable title = "资产领用";
	@observable loading = false;
	@observable visible = false;
    @observable tableInfo = {};
    @observable currentpage = {} ; // 页面路由
	@observable submitDatas = [];  //暂存数据
	@observable verified = false;
	@observable hasRight = false; //权限
	@observable viewtype = "";
	@observable remind;
	@observable batchBrowser;
    @action 
    //请求数据
    initTableInfo =(params ={})=>{
        this.loading = true;
        this.currentpage = params;
		const viewtype = params.viewtype;
		this.viewtype = params.viewtype;
        this.submitDatas = [];
		this.tableInfo = {};
		this.verified = false;
		this.hasRight = false; //权限
        Apis.cptManager(params).then(data=>{
			if(data.isright == "false"  && typeof(data.isright) !== "undefined"){
				this.hasRight = false;
				this.verified = true;
			}else{
				this.hasRight = true;
				this.verified = true;
				this.tableInfo = data.subTableInfo;
				this.submitDatas = data.subTableInfo.columnDefine;
				this.batchBrowser = data.batchBrowser;
			}
            this.loading = false;
        })
    }
    //保存添加数据
    saveTableInfoDatas = (datas) =>{
        this.submitDatas = datas;
    }
    //提交
    doSubmit = () =>{
        const viewtype = this.currentpage.viewtype;
        if(this.submitDatas.length==0){
			message.error(getLabel('33027',"请至少输入1条明细记录！"));
		}else{
			this.visible = true;
			if("cptuse"==viewtype){
				this.doCptUse();
			}else if("cptmove"==viewtype){
				this.doCptMove();
			}else if("cptlend"==viewtype){
				this.doCptLend();
			}else if("cptloss"==viewtype){
				this.doCptLoss();
			}else if("cptdiscard"==viewtype){
				this.doCptDiscard();
			}else if("cptmend"==viewtype){
				this.doCptMend();
			}else if("cptback"==viewtype){
				this.doCptBack();
			}else if("cptchange"==viewtype){
				this.doCptChange();
			}
		}
    }
    //领用提交
    doCptUse = ()=>{
		let checkstr = ['capitalid','hrmid','capitalcount'];
        if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//处理数量为科学计数时，以字符串校验存在问题
				//poststr += "|"+item.capitalid+","+ item.capitalcount;
				poststr.push({capitalid:item.capitalid,capitalcount:item.capitalcount})
			});
			if(poststr.length>0){
                Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产调拨提交
    doCptMove = () =>{
		if(this.remind.showErrors()){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//poststr += "|"+item.capitalid+",1";
				poststr.push({capitalid:item.capitalid,capitalcount:item.availablenum})
			})

			if(poststr.length>0){
				Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产借用提交
    doCptLend = ()=>{
        let checkstr = ['hrmid','capitalid'];
		if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//poststr += "|"+item.capitalid+",1";
				poststr.push({capitalid:item.capitalid,capitalcount:1})
			})

			if(poststr.length>0){
				Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产减损提交
    doCptLoss = () =>{
        let checkstr = ['capitalcount','capitalid'];
		if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//poststr += "|"+item.capitalid+","+item.capitalcount;
				poststr.push({capitalid:item.capitalid,capitalcount:item.capitalcount})
			})

			if(poststr.length>0){
				Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产报废提交
    doCptDiscard = () =>{
		let checkstr = ['capitalcount','capitalid'];
		if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//poststr += "|"+item.capitalid+","+item.capitalcount;
				poststr.push({capitalid:item.capitalid,capitalcount:item.capitalcount})
			})

			if(poststr.length>0){
				Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产送修提交
    doCptMend = () =>{
		let checkstr = ['capitalid'];
		if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//poststr += "|"+item.capitalid+",1";
				poststr.push({capitalid:item.capitalid,capitalcount:1})
			})

			if(poststr.length>0){
				Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产归还提交
    doCptBack = () =>{
		let checkstr = ['capitalid'];
		if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//poststr += "|"+item.capitalid+",1";
				poststr.push({capitalid:item.capitalid,capitalcount:1})
			})

			if(poststr.length>0){
				Apis.cptCheck({poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
    }
    //资产变更
    doCptChange = () =>{
		let checkstr = ['capitalid'];
		if(this.check_form(checkstr)){
			let subfields = toJS(this.submitDatas);
			let poststr = [];
			subfields.map((item) =>{
				//变更数量不填，不校验
				if(item.capitalnum){
					poststr.push({capitalid:item.capitalid,capitalcount:item.capitalnum})
				}
			})
			if(poststr.length>0){
				Apis.cptCheck({cpttype:'change',poststr:JSON.stringify(poststr)}).then(data => {
					if(data.msg==''){
						this.doCptManager();//校验后保存
					}else{
						message.error(data.msg);
						this.visible = false;
					}
				}).catch(err => message.error(err))
			}
		}else{
			this.visible = false;
		}
	}

    //必填校验
    check_form = (checkstr)=>{
		let subfields = toJS(this.submitDatas);
		let bool=0;
		subfields.map((item) =>{
			checkstr.map ((cstr) => {
				if(!item[cstr]){
					bool++;
				}
			})
		});
		if(bool > 0){
			this.remind.showErrors();
			return false;
		}else{
			return true;
		}
    }
    //提交保存
    doCptManager =()=>{
        let params = {};
        params.subfields = JSON.stringify(toJS(this.submitDatas));
        params.viewtype = this.currentpage.viewtype ;
        let amsg = '';
        if("cptuse"==this.currentpage.viewtype){
            amsg = getLabel(1378, '领用');
        }else if("cptmove"==this.currentpage.viewtype){
            amsg = getLabel(1437, '调拨');
        }else if("cptlend"==this.currentpage.viewtype){
            amsg = getLabel(15325, '借用');
        }else if("cptloss"==this.currentpage.viewtype){
            amsg = getLabel(6054, '减损');
        }else if("cptdiscard"==this.currentpage.viewtype){
            amsg = getLabel(1386, '报废');
        }else if("cptmend"==this.currentpage.viewtype){
            amsg = getLabel(83557, '送修');
        }else if("cptback"==this.currentpage.viewtype){
            amsg = getLabel(1384, '归还');
        }else if("cptchange"==this.currentpage.viewtype){
            amsg = getLabel(19899, '变更');
		}
		amsg += getLabel(84565,'成功!');
        Apis.doCptManager(params).then(data=>{
            if(data.msg==''){
				message.success(amsg);
				this.submitDatas = [];
				this.tableInfo = {};
                this.initTableInfo({'viewtype':this.currentpage.viewtype});
			}else{
				message.error(data.msg);
			}
			this.visible = false;
        })
    }
    //注销
    clearStatus = () =>{
        this.tableInfo = {};
        this.currentpage = {} ; 
		this.submitDatas = []; 
    }

}

const managerStore = new ManagerStore();
export default managerStore;